package com.java110.mall.system.task;

import com.java110.core.factory.GenerateCodeFactory;
import com.java110.core.log.LoggerFactory;
import com.java110.dto.product.ProductGroupOrderDto;
import com.java110.dto.product.ProductGroupOrderReturnDto;
import com.java110.dto.shop.ShopDto;
import com.java110.dto.storeOrder.RefundOrderDto;
import com.java110.dto.task.TaskDto;
import com.java110.intf.acct.IOnlinePayV1InnerServiceSMO;
import com.java110.intf.acct.IRefundOrderMoneyV1InnerServiceSMO;
import com.java110.intf.goods.*;
import com.java110.mall.system.quartz.TaskSystemQuartz;
import com.java110.po.product.ProductGroupOrderPo;
import com.java110.po.product.ProductGroupOrderReturnPo;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.DateUtil;
import com.java110.utils.util.ListUtil;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @program: MicroCommunity
 * @description: 定时任务处理 拼团失败订单
 * @author: wuxw
 * @create: 2020-06-15 13:35
 **/
@Component
public class DealGroupFailOrderTemplate extends TaskSystemQuartz {

    private static Logger logger = LoggerFactory.getLogger(DealGroupFailOrderTemplate.class);

    @Autowired
    private IProductGroupOrderV1InnerServiceSMO productGroupOrderV1InnerServiceSMOImpl;

    @Autowired
    private IProductGroupOrderReturnV1InnerServiceSMO productGroupOrderReturnV1InnerServiceSMOImpl;

    @Autowired
    private IRefundOrderMoneyV1InnerServiceSMO refundOrderMoneyV1InnerServiceSMOImpl;

    @Autowired
    private IOnlinePayV1InnerServiceSMO onlinePayV1InnerServiceSMOImpl;

    private static final int EXPIRE_IN = 7200;


    @Override
    protected void process(TaskDto taskDto) {
        logger.debug("开始生成拼团批次" + taskDto.toString());

        // 获取小区
        List<ShopDto> shopDtos = getAllShop();

        for (ShopDto shopDto : shopDtos) {
            try {
                doDealShopGroupFailOrder(taskDto, shopDto);
            } catch (Exception e) {
                logger.error("推送消息失败", e);
            }
        }
    }

    /**
     * 团购失败退单
     *
     * @param taskDto
     * @param shopDto
     */
    private void doDealShopGroupFailOrder(TaskDto taskDto, ShopDto shopDto) {


        ProductGroupOrderDto productGroupOrderDto = new ProductGroupOrderDto();
        productGroupOrderDto.setState(ProductGroupOrderDto.STATE_W);
        productGroupOrderDto.setExpireTime(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_A));
        productGroupOrderDto.setShopId(shopDto.getShopId());
        List<ProductGroupOrderDto> productGroupOrderDtos = productGroupOrderV1InnerServiceSMOImpl.queryProductGroupOrders(productGroupOrderDto);

        if(ListUtil.isNull(productGroupOrderDtos)){
            return;
        }



        for(ProductGroupOrderDto tmpProductGroupOrderDto: productGroupOrderDtos){
            try {
                doOneDealShopGroupFailOrder(tmpProductGroupOrderDto);
            }catch (Exception e){
                e.printStackTrace();
            }
        }


    }

    private void doOneDealShopGroupFailOrder(ProductGroupOrderDto tmpProductGroupOrderDto) {

        ProductGroupOrderPo productGroupOrderPo = new ProductGroupOrderPo();
        productGroupOrderPo.setOrderId(tmpProductGroupOrderDto.getOrderId());
        productGroupOrderPo.setState(ProductGroupOrderDto.STATE_F);
        int flag = productGroupOrderV1InnerServiceSMOImpl.updateProductGroupOrder(productGroupOrderPo);

        if(flag < 1){
            throw new CmdException("退费失败");
        }

        ProductGroupOrderReturnPo productGroupOrderReturnPo = new ProductGroupOrderReturnPo();
        productGroupOrderReturnPo.setReason("拼团失败，系统退款");
        productGroupOrderReturnPo.setOrderId(tmpProductGroupOrderDto.getOrderId());
        productGroupOrderReturnPo.setReturnMoney(tmpProductGroupOrderDto.getGroupPrice());
        productGroupOrderReturnPo.setShopId(tmpProductGroupOrderDto.getShopId());

        productGroupOrderReturnPo.setReturnId(GenerateCodeFactory.getGeneratorId("11"));
        productGroupOrderReturnPo.setReturnType(ProductGroupOrderReturnDto.RETURN_TYPE_CUSTOM);
        productGroupOrderReturnPo.setState(ProductGroupOrderReturnDto.STATE_C);
        productGroupOrderReturnPo.setReturnMoney("1.0");//这里后期改造
        productGroupOrderReturnPo.setStateMsg("拼团失败，系统退款");
        flag = productGroupOrderReturnV1InnerServiceSMOImpl.saveProductGroupOrderReturn(productGroupOrderReturnPo);

        if (flag < 1) {
            throw new CmdException("保存数据失败");
        }



        //todo 触发支付平台退款逻辑
        refundOrderMoneyV1InnerServiceSMOImpl.refundOrderMoney(new RefundOrderDto(tmpProductGroupOrderDto.getOrderId(),Double.parseDouble(tmpProductGroupOrderDto.getGroupPrice())));

//        OnlinePayPo onlinePayPo = new OnlinePayPo();
//        onlinePayPo.setMessage("拼团失败，系统退款");
//        onlinePayPo.setOrderId(tmpProductGroupOrderDto.getOrderId());
//        onlinePayPo.setRefundFee(tmpProductGroupOrderDto.getGroupPrice());
//        onlinePayPo.setState(OnlinePayDto.STATE_WT);
//        onlinePayV1InnerServiceSMOImpl.updateOnlinePay(onlinePayPo);


    }


}
